package dao.impl;

import dao.TeacherDao;
import entity.Teacher;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;

public class TDaoImpl extends BaseDao implements TeacherDao {
    @Override
    public List<Teacher> getTeacherList() throws Exception {
        connection=getConnection();
        String sql="select * from teacher";
        /* statement = connection.prepareStatement(sql);
         resultSet = statement.executeQuery();*/
        resultSet=query(sql,null);//查询数据库
        ArrayList<Teacher> list = new ArrayList<>();
        while (resultSet.next()){
            int tno = resultSet.getInt("tno");
            String tname = resultSet.getString("tname");
            String gender = resultSet.getString("gender");
            Date drithdata = resultSet.getDate("birthdate");
            Teacher teacher = new Teacher(tno,tname,gender,drithdata);
            list.add(teacher);
        }
        return list;
    }




    public int addTeacher(Teacher teacher) throws Exception {
        connection=getConnection();
        String sql = "insert into teacher(tno,tname,gender,birthdate) values (?,?,?,?)";
         statement = connection.prepareStatement(sql);
        Object[] objects = new Object[4];
        objects[0]=teacher.getTno();
        objects[1]=teacher.getTname();
        objects[2]=teacher.getDenger();
        Timestamp timestamp = new Timestamp(teacher.getBirthday().getTime());
        objects[3]=timestamp;

        int update = super.update(sql,objects);
        /*statement.setObject(1, teacher.getTno());
        statement.setObject(2, teacher.getTname());
        statement.setObject(3, teacher.getDenger());
        Timestamp timestamp = new Timestamp(teacher.getBirthday().getTime());
        statement.setObject(4, timestamp);
        int i = statement.executeUpdate();*/
        close(connection, statement);
        return update;
    }
/*根据编号查询教师*/
    @Override
    public Teacher getTeacherByTno(int tno) throws Exception {
        connection=super.getConnection();
        String sql="select * from teacher where tno=?";
        Object[] objects={tno};
         resultSet = query(sql, objects);//查询数据库
      /*  statement=connection.prepareStatement(sql);
        statement.setObject(1,objects[0]);
         resultSet = statement.executeQuery();*/
        Teacher teacher = null;
        if (resultSet.next()){
             int tno1 = resultSet.getInt("tno");
             String tname = resultSet.getString("tname");
             String gender = resultSet.getString("gender");
             Date birthdate = resultSet.getDate("birthdate");
              teacher = new Teacher(tno, tname, gender,birthdate);
         }


        return teacher;
    }
}
